package com.xc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xc.domain.pojo.SysDept;
import com.xc.service.SysDeptService;
import com.xc.mapper.SysDeptMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept>
    implements SysDeptService{

    @Override
    public List<SysDept> ChildTreeDept() {

        List<SysDept> rootTree=new ArrayList<>();

        List<SysDept> sysDepts = this.baseMapper.selectList(null);

        for (SysDept sysDept:sysDepts){
            if (sysDept.getDeptId()==0){
                rootTree.add(sysDept);
            }
        }

        for (SysDept sysDept:rootTree){
            List<SysDept> childList=getChild(sysDept.getDeptId(),sysDepts);
            sysDept.setChildren(childList);
        }
        return rootTree;
    }

    private List<SysDept> getChild(Long deptId, List<SysDept> sysDepts) {
        //子菜单
        List<SysDept> childList=new ArrayList<>();
        for (SysDept sysDept:childList){
            if (sysDept.getParentId().equals(deptId)){
                childList.add(sysDept);
            }
        }

        for (SysDept sysDept:childList){
            sysDept.setChildren(getChild(sysDept.getDeptId(),sysDepts));
        }

        if (childList.size()==0){
            return new ArrayList<>();
        }
        return childList;
    }
}




